angular.module('tab-home', [])

.controller 'TabHomeCtrl', ($location, userData, $scope, $ionicLoading, $anchorScroll, $timeout, $state, $http, $ionicHistory, $ionicBackdrop, $ionicPlatform
, Toast, $localStorage) ->
    $scope.$storage = $localStorage
    $scope.ssh = {name:undefined}
    $localStorage.currentHos = undefined
    $scope.mark = false
    #获取全部医院接口
    getHosList = ->
        $http.get('/unit/all')
        .success (resp)->
#            console.log(resp);
            $scope.hosList = resp.message
            if $scope.hosList.length >0
                if  $localStorage.currentHos ==undefined
                    $localStorage.currentHos = $scope.hosList[0]
                getProductList()
    getProductList = ->
        $http.get('/product', params: {unitId: $localStorage.currentHos.id})
        .success (resp) ->
            if resp.code == 0
                $scope.proList = resp.message.content
            else
                Toast.showBusinessError(resp.message)
        .error (resp) ->
            Toast.showSysError()

    #登陆获取token,useid接口
    getUserInfo = ->
        $http.get('/customer/detail').success (resp)->
            if resp.code == 0
                $scope.user = resp.message
                userData.set($scope.user)
                $localStorage.userId = resp.message.id
            else
                Toast.showMsg("获取数据失败")
        .error (resp, status) ->
            console.log(resp)
            Toast.showMsg("加载数据失败")
    getTokenFromCurrentUrl = () ->
        token = $location.search().token
        $localStorage['openId'] = $location.search().openId if $location.search().openId
        if token != undefined
            userData.setToken(token)
            alert(token)
        getUserInfo()
        getHosList()
    getTokenFromCurrentUrl()

    _.extend $scope,
        yuyue: (item) ->
            $localStorage.getProinfo = item
            $state.go('tab.yuyue')
#点击开启/关闭医院选择页面
        changeMask: ->
            $scope.mask = !$scope.mask
            getHosList()
            $location.hash('zhezhao')
            $anchorScroll()
#选择医院事件
        chosen: (item) ->
            $localStorage.currentHos = item
            getProductList()
            $scope.mask = !$scope.mask
#搜索医院接口
        search: ->
            $http.get('/unit', params: {name: $scope.ssh.name})
            .success (resp) ->
                $scope.hosList = resp.message.content
            .error (err) ->
                Toast.showSysError()